package com.tzbank.back.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tzbank.back.service.ProductEvaluationService;
import com.tzbank.entity.backstage.ProductEvaluation;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
/**
 * 产品评价配置
 * @author 蔡宇钿
 *2018年5月29日
 *更新 蔡宇钿 2018.6.1
 */
@Slf4j
@RestController
public class ProductEvaluationController {
	
	@Autowired
	private ProductEvaluationService productEvaluationService;

	@GetMapping(value = "getlistProduct")
	// 查询全部
//	public Map<String, Object> getlistProduct(HttpServletRequest request) {
//		try {
//			return this.productEvaluationService.getlistProduct();
//		} catch (Exception e) {
//			log.error("ProductEvaluationController getlistProduct: ", e);
//			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
//		}
//	}
//
//	@GetMapping(value = "getlistByapplication")
	// 按照application查询
	public Map<String, Object> getlistByapplication(HttpServletRequest request, @RequestParam(required = false) String application) {
		try {
			if (null == application || application.equals("")) {
				return this.productEvaluationService.getlistProduct();
			}else {
				return this.productEvaluationService.getlistByapplication(application);
			}
			
		} catch (Exception e) {
			log.error("ProductEvaluationController getlistByapplication: ", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	@PostMapping(value = "addProduct")
	// 增加配置
	public Map<String, Object> addProduct(HttpServletRequest request, @RequestBody ProductEvaluation cptcInfoJson) {
		try {
			cptcInfoJson.toString();
			System.out.println(cptcInfoJson.toString());
//			cptcInfoJson.put("operator", "userId");
			return this.productEvaluationService.addProduct(cptcInfoJson);
		} catch (Exception e) {
			log.error("ProductEvaluationController addProduct: ", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
		
	}

	@PostMapping(value = "modifyByproductautoId")
	//更新配置
	public Map<String, Object> modifyByproductautoId(HttpServletRequest request, @RequestBody ProductEvaluation cptcInfoJson) {
		try {
//			cptcInfoJson.setOperator(request.getUserPrincipal());
			return this.productEvaluationService.modifyByproductautoId(cptcInfoJson);
		} catch (Exception e) {
			log.error("ProductEvaluationController modifyByproductautoId: ", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}

	@PostMapping(value = "deleteByproductautoId")
	// 通过逻辑方式实现删除
	public Map<String, Object> deleteByproductautoId(HttpServletRequest request, @RequestBody ProductEvaluation cptcInfoJson) {
		try {
			return this.productEvaluationService.deleteByproductautoId(cptcInfoJson);
		} catch (Exception e) {
			log.error("ProductEvaluationController deleteByproductautoId: ", e);
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
	}
}
